/*
 * Class Name:    DebugTools.java
 * Description:   TODO(类的功能描述)
 * Version:       2014-6-13 下午3:33:49
 * Author:        ray
 * Copyright 2010 Cennavi Corp, All Rights Reserved.
 */
package cn.com.cennavi.dass.uti;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

//import cn.com.cennavi.dass.Baidu.BaiduGPS;
//import cn.com.cennavi.dass.data.Stldataobject;

import com.cennavi.mm.thrift.GPS;

/**
 * TODO(这里用一句话描述这个类的作用)
 * 
 * @author ray
 * @version 2014-6-13 下午3:33:49
 */
public class DebugTools {

	private static StringBuffer sb = new StringBuffer();
	private static boolean DEBUG = false;
	private static final String COMMA = ",";
	private static final String SEPERA = "$";
	private static final String RTAG = "R";
	/**
	 * TODO(功能描述)
	 * 
	 * @param carID
	 * @param mmGPSList
	 * @param string
	 */
	public static void saveGpsToFile(String carID,
			List<com.cennavi.mm.thrift.GPS> mmGPSList, String folder) {
		// TODO Auto-generated method stub
		File dir = new File(folder + "RouteFailure//");
		if (!dir.isDirectory()) {
			dir.mkdirs();
		}
		File file = new File(folder + "RouteFailure//RouteFailure.txt");
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
			bw.write(carID);
			for (GPS gps : mmGPSList) {
				bw.write("|" + gps.longtitude + "," + gps.latitude + ","
						+ gps.speed + "," + gps.direction + "," + gps.time);
			}
			bw.newLine();
			bw.flush();
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * TODO(功能描述)
	 * 
	 * @param mmGPSList
	 * @param ddTrajectoryFolderPath
	 */
	public static void saveGpsToFile(List<com.cennavi.mm.thrift.GPS> mmGPSList,
			String folder) {
		// TODO Auto-generated method stub
		File dir = new File(folder + "RouteFailure//");
		if (!dir.isDirectory()) {
			dir.mkdirs();
		}
		File file = new File(folder + "RouteFailure//RouteFailure.txt");
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
			int count = 0;
			for (GPS gps : mmGPSList) {
				if (count != 0) {
					bw.write("|");
				}
				count++;
				bw.write(gps.time + "," + gps.longtitude + "," + gps.latitude);
			}
			bw.newLine();
			bw.flush();
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * TODO(功能描述)
	 * 
	 * @param carID
	 * @param mmGPSList
	 * @return
	 */
	public static String saveGpsToFile(String carID,
			List<com.cennavi.mm.thrift.GPS> mmGPSList) {
		// TODO Auto-generated method stub
		sb.setLength(0);
		sb.append(carID);
		for (GPS gps : mmGPSList) {
			sb.append("|");
			sb.append(gps.longtitude);
			sb.append(",");
			sb.append(gps.latitude);
			sb.append(",");
			sb.append(gps.speed);
			sb.append(",");
			sb.append(gps.direction);
			sb.append(",");
			sb.append(gps.time);
		}
		return sb.toString();
	}

	/**
	 * TODO(功能描述)
	 * 
	 * @param carID
	 * @param mmGPSList
	 * @return
	 */
	public static String saveGpsToFile(String carID,
			List<com.cennavi.mm.thrift.GPS> mmGPSList,
			List<com.cennavi.mm.thrift.GPS> rawGPSList) {
		// TODO Auto-generated method stub
		sb.setLength(0);
		sb.append(carID);
		for (GPS gps : mmGPSList) {
			sb.append("|");
			sb.append(gps.longtitude);
			sb.append(",");
			sb.append(gps.latitude);
			sb.append(",");
			sb.append(gps.speed);
			sb.append(",");
			sb.append(gps.direction);
			sb.append(",");
			sb.append(gps.time);
		}
		return sb.toString();
	}

	public static void print(String info) {
		if (DEBUG) {
			//System.out.print(info);
		}
	}

	public static void println(String info) {
		if (DEBUG) {
			//System.out.println(info);
		}
	}

//	/**
//	 * TODO(功能描述)
//	 * 
//	 * @param carID
//	 * @param rawlist
//	 * @return
//	 */
//	public static String saveGpsToFile(String carID, ArrayList<BaiduGPS> rawlist) {
//		// TODO Auto-generated method stub
//		sb.setLength(0);
//		sb.append(carID);
//		for (BaiduGPS gps : rawlist) {
//			sb.append("|");
//			sb.append(gps.lon);
//			sb.append(",");
//			sb.append(gps.lat);
//			sb.append(",");
//			sb.append(gps.speed);
//			sb.append(",");
//			sb.append(gps.direction);
//			sb.append(",");
//			sb.append(gps.time);
//			sb.append(",");
//			sb.append(gps.status);
//		}
//		return sb.toString();
//	}
//
//	/**
//	 * TODO(功能描述)
//	 * 
//	 * @param carID
//	 * @param rawlist
//	 * @return
//	 */
//	public static String saveGpsToRawFile(String carID, String uuid,
//			ArrayList<BaiduGPS> rawlist) {
//		// TODO Auto-generated method stub
//		sb.setLength(0);
//		sb.append(RTAG);
//		sb.append(SEPERA);
//		sb.append(uuid);
//		sb.append(SEPERA);
//		sb.append(carID);
//		for (BaiduGPS gps : rawlist) {
//			sb.append("|");
//			sb.append(gps.lon);
//			sb.append(",");
//			sb.append(gps.lat);
//			sb.append(",");
//			sb.append(gps.speed);
//			sb.append(",");
//			sb.append(gps.direction);
//			sb.append(",");
//			sb.append(gps.time);
//			sb.append(",");
//			sb.append(gps.status);
//		}
//		
//		return sb.toString();
//	}

	/**
	 * TODO(功能描述)
	 * 
	 * @param carID
	 * @param status 
	 * @param rawlist
	 * @return
	 */
	public static String saveGpsToTrajFile(String carID, String grid, String uuid, ArrayList<GPS> gpslist, int status) {
		// TODO Auto-generated method stub
		sb.setLength(0);
		sb.append(carID);
		sb.append(SEPERA);
		if (grid != null) {
			sb.append(grid);
			sb.append(SEPERA);
		}
		sb.append(uuid);
		sb.append(SEPERA);
		for (GPS gps : gpslist) {
			sb.append(gps.longtitude);
			sb.append(",");
			sb.append(gps.latitude);
			sb.append(",");
			sb.append(gps.speed);
			sb.append(",");
			sb.append(gps.direction);
			sb.append(",");
			sb.append(gps.time);
			sb.append(",");
			sb.append(status);
			sb.append("|");
		}
		sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}

//	/**
//	 * TODO(功能描述)
//	 * 
//	 * @param carID
//	 * @param rawlist
//	 * @return
//	 */
//	public static String saveGpsToFile(String carID, String gridId,
//			ArrayList<GPS> gpslist, ArrayList<BaiduGPS> rawlist) {
//		// TODO Auto-generated method stub
//		sb.setLength(0);
//		sb.append(carID);
//		sb.append("$");
//		sb.append(gridId);
//		sb.append("$");
//		for (GPS gps : gpslist) {
//			sb.append(gps.longtitude);
//			sb.append(",");
//			sb.append(gps.latitude);
//			sb.append(",");
//			sb.append(gps.speed);
//			sb.append(",");
//			sb.append(gps.direction);
//			sb.append(",");
//			sb.append(gps.time);
//			sb.append("|");
//		}
//		sb.deleteCharAt(sb.length() - 1);
//		sb.append("$");
//		for (BaiduGPS gps : rawlist) {
//			sb.append(gps.lon);
//			sb.append(",");
//			sb.append(gps.lat);
//			sb.append(",");
//			sb.append(gps.speed);
//			sb.append(",");
//			sb.append(gps.direction);
//			sb.append(",");
//			sb.append(gps.time);
//			sb.append(",");
//			sb.append(gps.status);
//			sb.append("|");
//		}
//		return sb.toString();
//	}
//
//	public static String saveSTLToRawFile(String carID, String uuid,
//			ArrayList<Stldataobject> rawlist) {
//		// TODO Auto-generated method stub
//		sb.setLength(0);
//		sb.append(RTAG);
//		sb.append(SEPERA);
//		sb.append(uuid);
//		sb.append(SEPERA);
//		sb.append(carID);
//		for (Stldataobject gps : rawlist) {
//			sb.append("|");
//			sb.append(gps.BJLongitude);
//			sb.append(",");
//			sb.append(gps.BJLatitude);
//			sb.append(",");
//			sb.append(gps.Speed);
//			sb.append(",");
//			sb.append(gps.Heading);
//			sb.append(",");
//			sb.append(gps.TimeStamp);
//			sb.append(",");
//			sb.append(gps.Status);
//		}
//		
//		return sb.toString();
//	}

	public static String saveGpsToRawFileWithoutStatus(String carID, String uuid,
			ArrayList<GPS> rawlist) {
		// TODO Auto-generated method stub
		sb.setLength(0);
		sb.append(RTAG);
		sb.append(SEPERA);
		sb.append(uuid);
		sb.append(SEPERA);
		sb.append(carID);
		for (GPS gps : rawlist) {
			sb.append("|");
			sb.append(gps.longtitude);
			sb.append(",");
			sb.append(gps.latitude);
			sb.append(",");
			sb.append(gps.speed);
			sb.append(",");
			sb.append(gps.direction);
			sb.append(",");
			sb.append(gps.time);
			sb.append(",");
			sb.append("-1");
		}
		
		return sb.toString();
	}

	public static String saveGpsToRawFile(String carID, String uuid,
			ArrayList<GPS> rawlist, int status) {
		// TODO Auto-generated method stub
		sb.setLength(0);
		sb.append(RTAG);
		sb.append(SEPERA);
		sb.append(uuid);
		sb.append(SEPERA);
		sb.append(carID);
		for (GPS gps : rawlist) {
			sb.append("|");
			sb.append(gps.longtitude);
			sb.append(",");
			sb.append(gps.latitude);
			sb.append(",");
			sb.append(gps.speed);
			sb.append(",");
			sb.append(gps.direction);
			sb.append(",");
			sb.append(gps.time);
			sb.append(",");
			sb.append(status);
		}
		
		return sb.toString();
	}

}
